package com.selfdriving.wl.activity;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RadioButton;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.adapter.RankingAdapter;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.Ranking;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.LogUtil;
import com.selfdriving.utils.Utils;

public class BestFeaturePoplular implements OnClickListener
{
	/** 当前View **/
	private View view;
	private BaseApplication application;
	private Context context;

	private ListView feature_list;

	public RadioButton btnLeft;

	private RadioButton btnRight;

	public RankingAdapter adapter;

	private List<Entity> features = new ArrayList<Entity>();

	public BestFeaturePoplular(BaseApplication application, Context context)
	{
		LayoutInflater inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.w_pager_popular_list, null);
		this.application = application;
		this.context = context;
		initViews();
	}

	public void initViews()
	{
		feature_list = (ListView) view.findViewById(R.id.feature_list);

		btnLeft = (RadioButton) view.findViewById(R.id.ivLeft);

		btnRight = (RadioButton) view.findViewById(R.id.ivRight);

		btnLeft.setOnClickListener(this);

		btnRight.setOnClickListener(this);

		adapter = new RankingAdapter(application, context, features);

		feature_list.setAdapter(adapter);

		btnLeft.setSelected(true);

		initPagerLeft();
	}

	public View getView()
	{
		return view;
	}

	private void initPagerLeft()
	{
		features.clear();
		adapter.notifyDataSetChanged();
		getPopularListLeft();
	}

	private void initPagerRight()
	{
		features.clear();
		adapter.notifyDataSetChanged();
		getPopularRight();
	}

	private void getPopularListLeft()
	{
		AjaxParams params = new AjaxParams();
		params.put("f", "77");
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				try
				{
					JSONArray array = new JSONArray(json);
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject object = array.getJSONObject(i);
						String id = object.getString("id");
						String click = object.getString("click");
						String name = object.getString("name");
						features.add(new Ranking(id, name, click));
					}
					Utils.dismissLoadingDialog();
					feature_list.setAdapter(adapter);
				} catch (JSONException e)
				{
					Utils.dismissLoadingDialog();
					e.printStackTrace();
				}
			}
		});
	}

	private void getPopularRight()
	{
		AjaxParams params = new AjaxParams();
		params.put("f", "78");
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				try
				{
					JSONArray array = new JSONArray(json);
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject object = array.getJSONObject(i);
						String id = object.getString("id");
						String click = null;
						String name = object.getString("name");
						features.add(new Ranking(id, name, click));
					}
					Utils.dismissLoadingDialog();
					feature_list.setAdapter(adapter);
				} catch (JSONException e)
				{
					Utils.dismissLoadingDialog();
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		switch (v.getId())
		{
		case R.id.ivLeft:
			LogUtil.e("左边");
			initPagerLeft();
			btnLeft.setChecked(true);
			btnRight.setChecked(false);
			break;
		case R.id.ivRight:
			LogUtil.e("右边");
			initPagerRight();
			btnLeft.setChecked(false);
			btnRight.setChecked(true);
			break;
		}
	}

}
